﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Subsonic.Data;
using MichMan.Utilities.Requests;

namespace Subsonic.ViewModels
{
    public class AlbumListItemViewModel : BasePivotItemViewModel<Group<AlbumListChildItem>>
    {
        public AlbumListItemViewModel(AlbumListType albumListType)
        {
            AlbumListType = albumListType;
            Title = String.Format("{{main_albums_{0}}}", AlbumListType.ToString());
            Id = Title;
        }

        AlbumListType AlbumListType { get; set; }

        public override IRequest LoadItems()
        {
            GetAlbumListRequest getAlbumLisRequest = new GetAlbumListRequest(AlbumListType);
            getAlbumLisRequest.RunRequestCompleted += new RunRequestCompletedEventHandler(albumListRequest_RunRequestCompleted);
            getAlbumLisRequest.RunRequestAsync();
            return getAlbumLisRequest;
        }

        void albumListRequest_RunRequestCompleted(object sender, MichMan.Utilities.Requests.RunRequestCompletedEventArgs e)
        {
            GetAlbumListRequest request = (GetAlbumListRequest)sender;
            if (request.Response == null || request.RequestStatus != RequestStatus.Succeeded)
            {
                // TODO:  Display error
                return;
            }

            Items.Add(new Group<AlbumListChildItem>(String.Empty, request.Response.AlbumList.Select(a => new AlbumListChildItem(AlbumListType, a))));
        }
    }
}
